package com.tanhua.dubbo.api;

import cn.hutool.core.collection.CollUtil;
import com.tanhua.model.mongo.UserLocation;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.geo.Circle;
import org.springframework.data.geo.Distance;
import org.springframework.data.geo.Metrics;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.geo.GeoJsonPoint;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;

import java.util.List;

@DubboService
public class UserLocationApiImpl implements UserLocationApi {

    @Autowired
    private MongoTemplate mongoTemplate;

    @Override//上传地理位置
    public Boolean  saveLocation(Long userId, double latitude, double longitude, String addrStr) {
        try {
            //1、根据用户id查询位置信息
                Query query =Query.query(Criteria.where("userId").is(userId));
            UserLocation one = mongoTemplate.findOne(query, UserLocation.class);
            //2、如果不存在用户位置信息，保存
            if(one==null){
                UserLocation userLocation=new UserLocation();
                userLocation.setAddress(addrStr);
                userLocation.setCreated(System.currentTimeMillis());
                userLocation.setLastUpdated(System.currentTimeMillis());
                userLocation.setUserId(userId);
                userLocation.setUpdated(System.currentTimeMillis());
                userLocation.setLocation(new GeoJsonPoint(longitude,latitude));
                mongoTemplate.save(userLocation);
            }else {
                //3、如果存在，更新
                Update update=Update.update("location",new GeoJsonPoint(longitude,latitude))
                        .set("lastUpdated",one.getUpdated())//上次修改时间
                        .set("updated",System.currentTimeMillis());
              mongoTemplate.updateFirst(query, update, UserLocation.class);
            }
            return true;
        } catch (Exception e) {
            return false;
        }

    }

    @Override//搜附近
    public List<Long> findByUserInfo(Long userId, String gender, Double aDouble) {
        //1、根据用户id，查询用户的位置信息
        Query query = Query.query(Criteria.where("userId").is(userId));
        UserLocation location = mongoTemplate.findOne(query, UserLocation.class);
        if(location == null) {
            return null;
        }
        //绘制原点
        GeoJsonPoint geoJsonPoint=location.getLocation();
        //3、绘制半径
        Distance distance=new Distance(aDouble/1000,Metrics.KILOMETERS);
        //4、绘制圆形
        Circle circle = new Circle(geoJsonPoint, distance);
        Query query1=Query.query(Criteria.where("location").withinSphere(circle));
        List<UserLocation> userLocations = mongoTemplate.find(query1, UserLocation.class);
        List<Long> ids = CollUtil.getFieldValues(userLocations, "userId", Long.class);
        return ids;
    }
}
